package com.example.fresh_market.adapter;

import android.support.annotation.NonNull;
import android.support.v7.recyclerview.extensions.ListAdapter;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.fresh_market.R;
import com.example.fresh_market.request.ProdCateResp;

public class ProdCateAdmAdapter extends ListAdapter<ProdCateResp, ProdCateAdmAdapter.ViewHolder> {

    private final OnItemClickListener clickListener;

    public ProdCateAdmAdapter(OnItemClickListener clickListener) {
        super(new DiffCallback());
        this.clickListener = clickListener;
    }

    protected ProdCateAdmAdapter(@NonNull DiffUtil.ItemCallback<ProdCateResp> diffCallback, OnItemClickListener clickListener) {
        super(diffCallback);
        this.clickListener = clickListener;
    }

    public ProdCateResp getItem(int position) {
        return super.getItem(position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cate_list_admin_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.bind(getItem(i));
        viewHolder.delete.setOnClickListener(v -> {
            clickListener.onDeleteClick(v, viewHolder.getAdapterPosition());
        });
        viewHolder.itemView.setOnClickListener(v -> {
            clickListener.onItemClick(v, viewHolder.getAdapterPosition());
        });
        viewHolder.edit.setOnClickListener(v -> {
            clickListener.onEditClick(v, viewHolder.getAdapterPosition());
        });
    }

    public interface OnItemClickListener {
        void onDeleteClick(View view, int position);

        void onEditClick(View view, int position);

        void onItemClick(View view, int position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView cateName;
        private final TextView createTime;
        private final TextView delete;
        private final TextView edit;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            cateName = itemView.findViewById(R.id.cateName);
            createTime = itemView.findViewById(R.id.cate_createTime);
            delete = itemView.findViewById(R.id.delete_cate);
            edit = itemView.findViewById(R.id.edit_cate);
        }

        protected void bind(ProdCateResp prodCateResp) {
            cateName.setText(String.format("类别名称：%s", prodCateResp.getCateName()));
            createTime.setText(String.format("创建时间：%s", prodCateResp.getCreateTime()));
        }
    }

    public static class DiffCallback extends DiffUtil.ItemCallback<ProdCateResp> {
        @Override
        public boolean areItemsTheSame(@NonNull ProdCateResp oldItem, @NonNull ProdCateResp newItem) {
            return oldItem.getId().equals(newItem.getId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull ProdCateResp oldItem, @NonNull ProdCateResp newItem) {
            return oldItem.equals(newItem);
        }
    }
}
